﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace Test_ApplicationBD
{
    public class CD
    {
        private List<Music> m_colMusic;
        private int m_ID;

        public int ID
        {
            get { return m_ID; }
            set { m_ID = value; }
        }
        private string m_name;

        public string Nom
        {
            get { return m_name; }
            set
            {
                m_name = value;
                Commandehelper.RequeteComplexe("update CD set Nom='" + Nom + "' where ID='" + ID + "';");
            }
        }

        private DateTime m_date;

        public DateTime Date
        {
            get { return m_date; }
            set { m_date = value; }
        }

        public CD()
        {

            m_date = DateTime.Now;
            m_name = "";
            m_colMusic = new List<Music>();

        }

        public void ModificationApporter()
        {
            
        }
        public static CD GetCD(int pID)
        {
            CD ObjCD = new CD();
            List<object[]> colObject = Commandehelper.RequeteComplexe("SELECT * from CD where Id="+pID+";");
            List<object[]> colMusique = Commandehelper.RequeteComplexe("SELECT * from CD_Musique where Id_CD=" + pID + ";");
            if (colObject == null)
                return null;
            ObjCD.ID = pID;
            for (int index = 0; index < colObject.Count; index++)
            {
                ObjCD.m_name = colObject[index][1].ToString();
                ObjCD.m_date = DateTime.Parse(colObject[index][2].ToString());
            }
            for (int i = 0; i < colMusique.Count; i++)
            {
                Music objmusic = new Music();
                objmusic.ChargerMusic((int)(colMusique[i][1]));
                ObjCD.m_colMusic.Add(objmusic);
            }

            return ObjCD;
        }

        public static CD CreateCD(string pNom, List<Music> pColMusic)
        {
            CD objCD = new CD();
            objCD.Nom = pNom;
            objCD.m_colMusic = pColMusic;
            Commandehelper.RequeteComplexe("insert into CD (Nom, DateCreation) values ('" + objCD.m_name + "','" + objCD.m_date.ToString("yyyy-MM-dd HH:mm:ss") + "');");
            objCD.ID = int.Parse(Commandehelper.RequeteSimple("select count(*) from CD;").ToString());
            for (int i = 0; i < objCD.m_colMusic.Count; i++)
            {
                Commandehelper.RequeteComplexe("insert into CD_Musique (Id_CD, Id_Musique) values ('" + objCD.ID + "', '" + objCD.m_colMusic[i].ID + "');");
            }
            return objCD;
        }

        public void GetCD(string pNom)
        {
            
        }

        public void GetCD(string pNom, DateTime pDate)
        {

        }
        public void AddMusique(Music pMusic)
        {
            m_colMusic.Add(pMusic);
            Commandehelper.RequeteComplexe("insert into CD_Musique (Id_CD, Id_Musique) values ('"+this.ID+"', '"+pMusic.ID+"');");
        }
        public Music musiqueAt(int index)
        {
            return m_colMusic[index];
        }
        public static void RemoveCD(int pID)
        {
            Commandehelper.RequeteComplexe("delete from CD where ID=" + pID + ";");
            Commandehelper.RequeteComplexe("delete from CD_Musique where Id_CD=" + pID + ";");
        }

        public static void DeleteTrackOf(int pIDCD, int pIdMusique)
        {
            Commandehelper.RequeteComplexe("delete from CD_Musique where Id_CD=" + pIDCD + " and Id_Musique=" + pIdMusique + ";");
        }
       
    }
}
